package flc.ast.dialog;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.UriUtils;
import com.stark.file.transfer.core.FileInfo;
import com.stark.file.transfer.core.TfContactInfo;
import com.stark.file.transfer.core.Transferable;
import com.stark.file.transfer.core.TransferableSendManager;
import com.stark.file.transfer.core.TransferableType;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.SelectFileBean;
import flc.ast.bean.TransferRecordBean;
import flc.ast.databinding.DialogProgressStyleBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.constant.FileType;

/**
 * @Author: XiaoDan
 * @Date: 2023/10/23
 * @Description:
 */
public class SendProgressDialog extends BaseSmartDialog<DialogProgressStyleBinding> implements TransferableSendManager.ISendListener {
    public boolean hasSuccess;
    public List<SelectFileBean> imgList, videoList, audioList, appList, contactList;


    public SendProgressDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_progress_style;
    }


    @Override
    protected void initView(View view) {
        imgList = new ArrayList<>();
        videoList = new ArrayList<>();
        audioList = new ArrayList<>();
        appList = new ArrayList<>();
        contactList = new ArrayList<>();

        mDataBinding.tvTitle.setText(R.string.file_transfer_text);
        TransferableSendManager manager = TransferableSendManager.getInstance();
        manager.setSendListener(this);
        manager.setSendListener1(new TransferableSendManager.ISendListener1() {
            @Override
            public void onCompleteItem(Transferable transferable, boolean success) {
                if (success) {
                    SelectFileBean bean = new SelectFileBean();
                    TransferableType type = transferable.getTransferType();
                    if (type == TransferableType.FILE) {
                        FileInfo fileInfo = (FileInfo) transferable;
                        if (fileInfo.getFileType() == FileType.IMAGE) {
                            bean.setType(1);
                            bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                            bean.setName(fileInfo.getFileName());
                            bean.setSize(fileInfo.getSize());
                            imgList.add(bean);
                        } else if (fileInfo.getFileType() == FileType.VIDEO) {
                            bean.setType(2);
                            bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                            bean.setName(fileInfo.getFileName());
                            bean.setSize(fileInfo.getSize());
                            videoList.add(bean);
                        } else if (fileInfo.getFileType() == FileType.AUDIO) {
                            bean.setType(3);
                            bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                            bean.setName(fileInfo.getFileName());
                            bean.setSize(fileInfo.getSize());
                            audioList.add(bean);
                        } else if (fileInfo.getFileType() == FileType.APK) {
                            bean.setType(4);
                            bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                            bean.setName(fileInfo.getFileName());
                            bean.setSize(fileInfo.getSize());
                            appList.add(bean);
                        }
                    } else if (type == TransferableType.CONTACT) {
                        TfContactInfo tfContactInfo = (TfContactInfo) transferable;
                        Log.e("test", "onReceivedTransferable: " + tfContactInfo.getContactInfoList().size());
                        for (ContactInfo contactInfo : tfContactInfo.getContactInfoList()) {
                            SelectFileBean contactBean = new SelectFileBean();
                            contactBean.setType(5);
                            contactBean.setName(contactInfo.getName());
                            contactBean.setPath(contactInfo.getPhone());
                            contactList.add(contactBean);
                        }
                    }
                }
            }
        });
    }


    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    @Override
    public void onCompleteCount(int totalCount, int haveSendCount) {
        mDataBinding.sbProgress.setMax(totalCount);
        mDataBinding.sbProgress.setProgress(haveSendCount);
        if (totalCount == haveSendCount) {
            hasSuccess = true;

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    List<TransferRecordBean> listSave = new ArrayList<>();
                    String time = TimeUtils.getNowString(new SimpleDateFormat("yyyy年MM月dd日 HH:mm"));
                    if (imgList.size() != 0) {
                        listSave.add(new TransferRecordBean(time, 1, imgList));
                    }
                    if (videoList.size() != 0) {
                        listSave.add(new TransferRecordBean(time, 2, videoList));
                    }
                    if (audioList.size() != 0) {
                        listSave.add(new TransferRecordBean(time, 3, audioList));
                    }
                    if (appList.size() != 0) {
                        listSave.add(new TransferRecordBean(time, 4, appList));
                    }
                    if (contactList.size() != 0) {
                        listSave.add(new TransferRecordBean(time, 5, contactList));
                    }
                    List<TransferRecordBean> listSp = AppSpUtils.getTransferRecordList();
                    if (listSp != null && listSp.size() > 0) {
                        listSp.addAll(0, listSave);
                        AppSpUtils.saveTransferRecordList(listSp);
                    } else {
                        AppSpUtils.saveTransferRecordList(listSave);
                    }

                    mDataBinding.sbProgress.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                            if (listener != null) {
                                listener.clickConfirm();
                            }
                        }
                    }, 500);
                }
            }, 1000);
        }
    }

    public interface Ilistener {
        void clickConfirm();
    }


}
